<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-10-10
 * Time: 上午8:21
 * @author 郑钟良<zzl@ourstu.com>
 */

namespace Admin\Controller;


use Admin\Builder\AdminListBuilder;

class CampusController extends AdminController
{
    //校园成员
    /**校园成员列表
     * @param int $page
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function member($page = 1)
    {
        $memberIds = D('AuthGroupAccess')->where(array('group_id' => 2))->field('uid')->page($page, 50)->select(); //group_id=2是校园用户组id
        $totalCount = D('CampusAudit')->where(array('group_id' => 2))->count();
        $memberList = array();
        foreach ($memberIds as $v) {
            $memberList[] = query_user(array('id', 'uid', 'reg_time', 'last_login_time', 'email', 'is_recommend', 'is_production', 'fans', 'doing', 'address', 'sentiment', 'help_count'), $v['uid']);
        }
        unset($v);
        $builder = new AdminListBuilder();

        $builder->title('校园用户列表')->meta_title = '校园用户列表';
        $builder->ajaxButton(U('admin/campus/setRecommend'), array('is_recommend' => 1), '设为推荐')
            ->keyUid()->keyTime('reg_time', '注册时间')->keyTime('last_login_time', '最后登录时间')->keyText('email', '邮箱')->keyYesNo('is_recommend', '是否推荐')->keyYesNo('is_production', '是否为制片成员')->keyText('fans', '粉丝数')->keyText('doing', '职业')->keyText('address', '居住地址')->keyText('sentiment', '人气')->keyText('help_count', '帮忙数');
        $builder->pagination($totalCount, 50)->data($memberList)
            ->display();
    }

    /**推荐成员列表
     * @param int $page
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function recommendMember($page = 1)
    {
        $memberIds = D('AuthGroupAccess')->where(array('group_id' => 2))->field('uid')->select(); //group_id=2是校园用户组id
        $memberIds = array_column($memberIds, 'uid');
        $map['status'] = 1;
        $map['last_login_time'] = array('neq', 0);
        $map['uid'] = array('in', $memberIds);
        $map['is_recommend'] = 1;
        $recommendIds = D('Member')->where($map)->field('uid')->page($page, 50)->select();
        $totalCount = D('Member')->where($map)->count();
        $memberList = array();
        foreach ($recommendIds as $v) {
            $memberList[] = query_user(array('id', 'uid', 'reg_time', 'last_login_time', 'email', 'is_recommend', 'is_production', 'fans', 'doing', 'address', 'sentiment', 'help_count'), $v['uid']);
        }
        unset($v);
        $builder = new AdminListBuilder();

        $builder->title('校园用户列表')->meta_title = '校园用户列表';
        $builder->ajaxButton(U('admin/campus/setRecommend'), array('is_recommend' => 0), '取消推荐')
            ->keyUid()->keyTime('reg_time', '注册时间')->keyTime('last_login_time', '最后登录时间')->keyText('email', '邮箱')->keyYesNo('is_recommend', '是否推荐')->keyYesNo('is_production', '是否为制片成员')->keyText('fans', '粉丝数')->keyText('doing', '职业')->keyText('address', '居住地址')->keyText('sentiment', '人气')->keyText('help_count', '帮忙数');
        $builder->data($memberList)->pagination($totalCount, 50)
            ->display();
    }

    /**设置、取消推荐
     * @param string $ids
     * @param int $is_recommend
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function setRecommend($ids = '', $is_recommend = 1)
    {
        if (!is_array($ids) || !count($ids)) {
            $this->error('请勾选要设置的项！');
        }
        D('Member')->where(array('uid' => array('in', $ids)))->setField('is_recommend', $is_recommend);
        foreach ($ids as $val) {
            clean_query_user_cache($val, 'is_recommend');
        }
        $this->success('设置成功', $_SERVER['HTTP_REFERER']);
    }

    /**待审核
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function waitAudit($page = 1)
    {
        $waitIds = D('CampusAudit')->where(array('status' => 0))->field('id,uid')->page($page, 50)->select();
        $totalCount = D('CampusAudit')->where(array('status' => 0))->count();
        $waitList = array();
        foreach ($waitIds as $v) {
            $data = query_user(array('uid', 'reg_time', 'last_login_time', 'email'), $v['uid']);
            $data['id'] = $v['id'];
            $waitList[] = $data;
        }
        unset($v);
        $builder = new AdminListBuilder();

        $builder->title('待审核用户列表')->meta_title = '待审核用户列表';
        $builder->setStatusUrl(U('setStatus'))->buttonEnable('', '审核通过')->buttonDelete('', '审核不通过')
            ->keyUid()->keyTime('reg_time', '注册时间')->keyTime('last_login_time', '最后登录时间')->keyText('email', '邮箱');
        $builder->data($waitList)->pagination($totalCount, 50)
            ->display();
    }

    public function failureAudit($page = 1)
    {
        $waitIds = D('CampusAudit')->where(array('status' => -1))->field('id,uid')->page($page, 50)->select();
        $totalCount = D('CampusAudit')->where(array('status' => -1))->count();
        $waitList = array();
        foreach ($waitIds as $v) {
            $data = query_user(array('uid', 'reg_time', 'last_login_time', 'email'), $v['uid']);
            $data['id'] = $v['id'];
            $waitList[] = $data;
        }
        unset($v);
        $builder = new AdminListBuilder();

        $builder->title('审核失败用户列表')->meta_title = '审核失败用户列表';
        $builder->setStatusUrl(U('setStatus'))->buttonEnable('', '审核通过')
            ->keyUid()->keyTime('reg_time', '注册时间')->keyTime('last_login_time', '最后登录时间')->keyText('email', '邮箱');
        $builder->data($waitList)->pagination($totalCount, 50)
            ->display();
    }

    public function setStatus($ids = '', $status = '')
    {
        if (!is_array($ids) || !count($ids)) {
            $this->error('请勾选要设置的项！');
        }
        $builder = new AdminListBuilder();
        if ($status == 1) {
            $data['group_id'] = 2; //group_id=2是校园用户组id
            $uids = D('CampusAudit')->where(array('id' => array('in', $ids)))->field('uid')->select();
            $uids = array_column($uids, 'uid');
            foreach ($uids as $val) {
                $data['uid'] = $val;
                if (!(D('AuthGroupAccess')->where($data)->find())) {
                    D('AuthGroupAccess')->add($data);
                    sendMessage($val,'你已成为校园成员','校园：校园成员');
                }
            }
            unset($data, $val);
        }
        $builder->doSetStatus('CampusAudit', $ids, $status);
    }

    //校园成员end

    //校园帮忙

    /**校园帮忙列表
     * @param int $page
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function product($page = 1)
    {
        $memberIds = D('AuthGroupAccess')->where(array('group_id' => 2))->field('uid')->select(); //group_id=2是校园用户组id
        $memberIds = array_column($memberIds, 'uid');
        $helpList = D('Help/Help')->getHelpListByUids($memberIds,$page,$totalCount);
        $dataList = array();
        $recommendIds = $this->getRecommendIds();
        //dump($helpList);exit;
        foreach ($helpList as $val) {
            $data['is_recommend'] = in_array($val['id'], $recommendIds) ? '1' : '0';
            $data['id'] = $val['id'];
            $data['title'] = $val['title'];
            $data['description'] = $val['description'];
            $data['state'] = get_help_condition($val['state']);
            $data['age'] = $val['s_age'].'-'.$val['e_age'];
            $data['height'] = $val['s_height'].'-'.$val['e_height'];
            $data['sex']=$val['sex']==1?'男':($val['sex']==0?'女':'不限');
            $data['address']=get_address($val['province'],$val['city'],$val['district']);
            $data['type']=get_professional($val['type']);
            $data['number']=$val['number'];
            $data['s_time'] = $val['s_time'];
            $data['e_time'] = $val['e_time'];
            $data['uid']=$val['uid'];

            $dataList[] = $data;
        }
        unset($val, $data);
        $builder = new AdminListBuilder();
        $builder->title('校园帮忙列表')->meta_title = '校园帮忙列表';
        $builder->ajaxButton(U('admin/campus/setProductRecommend'), array('recommend' => 1), '设为推荐');
        $builder->keyId('id', '帮忙ID')->keyLink('title', '帮忙标题', 'help/index/detail?id={$id}', '_black')->keyUid('uid','发布者')->keyBool('is_recommend','是否为推荐')->keyText('description', '详情')->keyText('state', '状态')->keyText('age', '年龄段')->keyText('height','身高段')->keyText('sex','性别要求')->keyText('address','地区要求')->keyText('type','职业要求')->keyText('number','人数限制')->keyTime('s_time', '开始时间')->keyTime('e_time', '结束时间');
        $builder->data($dataList)->pagination($totalCount,50)->display();
    }

    /**校园帮忙列表
     * @param int $page
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function productRecommend($page = 1)
    {
        $recommendIds = $this->getRecommendIds();
        $helpList = D('Help/Help')->getPage(array('id'=>array('in',$recommendIds)),$totalCount,$page);
        $dataList = array();
        foreach ($helpList as $val) {
            $data['id'] = $val['id'];
            $data['title'] = $val['title'];
            $data['description'] = $val['description'];
            $data['state'] = get_help_condition($val['state']);
            $data['age'] = $val['s_age'].'-'.$val['e_age'];
            $data['height'] = $val['s_height'].'-'.$val['e_height'];
            $data['sex']=$val['sex']==1?'男':($val['sex']==0?'女':'不限');
            $data['address']=get_address($val['province'],$val['city'],$val['district']);
            $data['type']=get_professional($val['type']);
            $data['number']=$val['number'];
            $data['s_time'] = $val['s_time'];
            $data['e_time'] = $val['e_time'];
            $data['uid']=$val['uid'];

            $dataList[] = $data;
        }
        unset($val, $data);
        $builder = new AdminListBuilder();
        $builder->title('推荐帮忙列表')->meta_title = '推荐帮忙列表';
        $builder->ajaxButton(U('admin/campus/setProductRecommend'), array('recommend' => 0), '取消推荐');
        $builder->keyId('id', '帮忙ID')->keyLink('title', '帮忙标题', 'help/index/detail?id={$id}', '_black')->keyUid('uid','发布者')->keyText('description', '详情')->keyText('state', '状态')->keyText('age', '年龄段')->keyText('height','身高段')->keyText('sex','性别要求')->keyText('address','地区要求')->keyText('type','职业要求')->keyText('number','人数限制')->keyTime('s_time', '开始时间')->keyTime('e_time', '结束时间');
        $builder->data($dataList)->pagination($totalCount,50)->display();
    }

    public function getRecommendIds()
    {
        $ids = D('CampusRecommend')->field('help_id')->select();
        $ids = array_column($ids, 'help_id');
        return $ids;
    }

    public function setProductRecommend($ids = '', $recommend = 1)
    {
        if (!is_array($ids) || !count($ids)) {
            $this->error('请勾选要设置的项！');
        }
        if ($recommend == 1) {
            $recommendIds = $this->getRecommendIds();
            $ids=array_diff($ids,$recommendIds);
            foreach ($ids as $val) {
                D('CampusRecommend')->add(array('help_id' => $val));
            }
        } else {
            D('CampusRecommend')->where(array('help_id' => array('in', $ids)))->delete();
        }
        $this->success('设置成功', $_SERVER['HTTP_REFERER']);
    }
    //校园帮忙end
}